package mobao.log.api.controller;

import lombok.extern.log4j.Log4j;
import mobao.log.api.entity.SysLogEntity;
import mobao.log.api.service.SysLogService;
import mobao.log.kafka.Provider;
import mobao.log.util.PageUtils;
import mobao.log.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/log")
@Log4j
public class SysLogController {
    @Autowired
	private SysLogService sysLogService;
	@Autowired
    private Provider provider;
	/**
	 * 列表
	 */
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public Result list(@RequestParam Map<String, Object> params){
		PageUtils page = sysLogService.queryPage(params);
		return Result.ok().put("page", page);
	}


	/**
	 * 保存
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public Result save(@RequestBody SysLogEntity entity){
		entity.setCreateDate(new Date());
		//插入
		log.info("保存log信息:"+entity.toString());
		sysLogService.insert(entity);
		//异步发送消息去清洗
		log.info("发送log信息到数据清洗仓库:"+entity.toString());
		provider.send("moboLog","摩宝DEMO的LOG日志进行清洗",entity);
		return Result.ok().put("sysLog",entity);
	};

}